﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EasyPiano
{
    public partial class PianoKeyControl : UserControl
    {
        int soundIndex = 0;
        Color orignColor = Color.White;
        int theme = 0;

        public int SoundIndex { get => soundIndex;
            set
            {
                soundIndex = value;
                snc.NumberText = Util.SoundIndexToString(value - GlobalData.offset);
            }
        }
        public Color OrignColor
        {
            get => orignColor;
            set
            {
                this.BackColor = value;
                orignColor = value;
                if (value == Color.Black)
                {
                    snc.SetTheme(1);
                }
            }            
        }

        public PianoKeyControl()
        {
            InitializeComponent();
        }

        private void PianoKeyControl_MouseDown(object sender, MouseEventArgs e)
        {
            PainoKeyDown();
            //解决鼠标移速过快，无法弹起的bug
            PianoKeyboardControl pianoKeyboard = (PianoKeyboardControl)this.Parent;
            pianoKeyboard.AddPressIndex(soundIndex);
            //用于滑动弹奏
            this.DoDragDrop(soundIndex, DragDropEffects.Move);
        }

        private void PianoKeyControl_MouseUp(object sender, MouseEventArgs e)
        {
            PainoKeyUp();
        }

        private void PianoKeyControl_Load(object sender, EventArgs e)
        {
            OrignColor = this.BackColor;
        }
        public void UpdateColor()
        {            
            this.BackColor = Color.Orange;
        }
        public void ResetColor()
        {
            this.BackColor = OrignColor;
        }
      
        private void PianoKeyControl_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move;
            if ((int)e.Data.GetData("System.Int32") != this.soundIndex)
            {                
                e.Data.SetData(-1);
                PainoKeyDown();
            }
        }

        private void PianoKeyControl_DragLeave(object sender, EventArgs e)
        {
            PainoKeyUp();
            ResetAllPianoKeys();
        }

        private void PianoKeyControl_DragDrop(object sender, DragEventArgs e)
        {
            PainoKeyUp();
            ResetAllPianoKeys();
        }

        public void PainoKeyUp()
        {
            ResetColor();
            this.Refresh();

            if (GlobalData.continueSound)
                Util.StopPlay(soundIndex);
        }

        public void PainoKeyDown()
        {
            UpdateColor();
            this.Refresh();

            if (GlobalData.continueSound == false)
                Util.AsynPlaySound(GlobalData.soundsPath[SoundIndex]);
            else
                Util.StartPlay(soundIndex);
        }

        void ResetAllPianoKeys()
        {
            PianoKeyboardControl pianoKeyboard = (PianoKeyboardControl)this.Parent;
            pianoKeyboard.ResetAllPianoKeys();
        }

        private void snc_MouseDown(object sender, MouseEventArgs e)
        {
            base.OnMouseDown(e);
        }

        private void snc_MouseUp(object sender, MouseEventArgs e)
        {
            base.OnMouseUp(e);
        }

        private void snc_Click(object sender, EventArgs e)
        {
            base.OnClick(e);
        }

        public void SetTheme(int theme)
        {
            this.theme = theme;
            if (theme == 1)
            {
                snc.Visible = true;
                snc.Enabled = true;                
            }
            else
            {
                snc.Visible = false;
                snc.Enabled = false;                
            }
        }
    }
}
